package com.tanhua.sso.controller;

import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("user")//一级URL路径
public class SmsController {

    @Autowired
    private SmsService smsService;

    /*
    * 短信认证
    * @RequestBody
    *   json格式字符串数据转换成对象类型
    * @ResponseBody
    *  对象类型转换成json格式字符串数据
    * */
    @PostMapping("login")
    public ResponseEntity<ErrorResult> sendCheckCode(@RequestBody Map<String,String> map){
        String phone=map.get("phone");
        //做手机验证码的校验
        ErrorResult result=null;
        try {
            result=smsService.sendCheckCode(phone);
            if (result==null){//ErrorResult是null的情况才是发送验证码成功
                return ResponseEntity.ok(null);
            }
        }catch (Exception e){
            result=ErrorResult.builder().errCode("000001").errMessage("xxxx").build();
        }
        return  ResponseEntity.status(500).body(result);
    }
}
